package com.itheima.d4_collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
    目标：斗地主游戏的案例开发。
    业务需求分析:
        业务: 总共有54张牌。
        点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2"
        花色: "♠", "♥", "♣", "♦"
        大小王: "👲" , "🃏"
        点数分别要组合4种花色，大小王各一张。
        斗地主：发出51张牌，剩下3张作为底牌。
 */
public class GameDemo {
    public static void main(String[] args) {
        //点数numbers
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //花色
        String[] colors = {"♠", "♥", "♣", "♦"};
        //大小
        int size = 0;
        //创建集合，存放牌
        ArrayList<Card> cards = new ArrayList<>();
        //获取点数
        for (String number : numbers) {
            //每次点数增大
            size++;
            for (String color : colors) {
                //创建对象，把点数，颜色，大小都传进去，添加到集合中
                Card card = new Card(number,color,size);
                cards.add(card);
            }
        }
      //创建两个牌对象存放大小王
        Card Xiaocard = new Card("","👲",++size);
        Card Dacard = new Card("","🃏",++size);

        Collections.addAll(cards,Xiaocard,Dacard);
        System.out.println(cards);


        //洗牌
        Collections.shuffle(cards);

        //3.发牌，因为有三个人，所以要创建三个集合
        ArrayList<Card> liubei = new ArrayList<>();
        ArrayList<Card> zhangfei = new ArrayList<>();
        ArrayList<Card> guanyu = new ArrayList<>();
         //要留3张底牌，所以-3
        for (int i1 = 0; i1 < cards.size()-3; i1++) {
            //获取每张牌
            Card card = cards.get(i1);
            //三个人获得牌
            if(i1%3==0){
                liubei.add(card);
            } else if (i1%3==1) {
                guanyu.add(card);
            } else{
                zhangfei.add(card);
            }
        }
        //对三个人获得的牌进行排序
        Collections.sort(liubei, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                //正序
                return o1.getSize()-o2.getSize();
            }
        });
        Collections.sort(guanyu, new Comparator<Card>() {
            //倒序
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getSize()-o1.getSize();
            }
        });
        Collections.sort(zhangfei, new Comparator<Card>() {
            //花色
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        });
        System.out.println("刘备"+liubei);
        System.out.println("关羽"+guanyu);
        System.out.println("张飞"+zhangfei);
        //截取
        List<Card> cards1 = cards.subList(cards.size() - 3, cards.size());
        System.out.println("这是底牌"+cards1);

        //抢地主，排序
        liubei.addAll(cards1);
        Collections.sort(liubei, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize()-o2.getSize();
            }
        });
        System.out.println(liubei);

    }
}










